BUTTERFLY_TANKSTELLEN_POSITIONS = {}
BUTTERFLY_TANKSTELLEN_MARKER = {}

local tankstelle_shop_marker = ""
local tankstelle_shopout_marker = ""

addEventHandler("onResourceStart", getResourceRootElement(), function()
	lade_alle_tankstellen_table()
	lade_alle_tankstellen_marker()
	lade_shop()
	--render_marker()
end)

function lade_alle_tankstellen_table()
	local tankstellenxml = xmlLoadFile("TANK/tankstellen.xml")
	if tankstellenxml then
		local xmlchild = xmlNodeGetChildren(tankstellenxml)
		for i, node in ipairs(xmlchild) do
			local posx = xmlNodeGetAttribute(node, "posx")
			local posy = xmlNodeGetAttribute(node, "posy")
			local posz = xmlNodeGetAttribute(node, "posz")
			local tankstellenname = xmlNodeGetAttribute(node, "tankstellenname")
			table.insert(BUTTERFLY_TANKSTELLEN_POSITIONS, {TABLE_POSX = posx, TABLE_POSY = posy, TABLE_POSZ = posz, TABLE_Tankstellenname = tankstellenname})
		end
	else
		outputChatBox("Tankstellen konnten nicht geladen werden! Grund: Tankstellen.xml konnte nicht geladen werden!", getRootElement(), 255, 30, 0)
	end
end

function lade_alle_tankstellen_marker()
	for i, wert in ipairs(BUTTERFLY_TANKSTELLEN_POSITIONS) do
		local POSX = BUTTERFLY_TANKSTELLEN_POSITIONS[i].TABLE_POSX
		local POSY = BUTTERFLY_TANKSTELLEN_POSITIONS[i].TABLE_POSY
		local POSZ = BUTTERFLY_TANKSTELLEN_POSITIONS[i].TABLE_POSZ
		local TANKSTELLENNAME = BUTTERFLY_TANKSTELLEN_POSITIONS[i].TABLE_Tankstellenname
		BUTTERFLY_TANKSTELLEN_MARKER[#BUTTERFLY_TANKSTELLEN_MARKER+1] = createMarker(POSX, POSY, POSZ-0.5, "corona", 1, 255, 120, 0, 255)
	end
end

function lade_shop()
	tankstelle_shop_marker = createMarker(2.029296875, -29.0087890625, 1003.2, "corona", 1, 120, 120, 0)
	setElementInterior(tankstelle_shop_marker, 10)
	tankstelle_shopout_marker = createMarker(6.0087890625, -31.7666015625, 1003.2, "corona", 1, 255, 120, 0)
	setElementInterior(tankstelle_shopout_marker, 10)
end

function render_marker()
	for i, marker in ipairs(BUTTERFLY_TANKSTELLEN_MARKER) do
		outputChatBox("CHECKA")
		triggerClientEvent("torendermarker_fuel", getRootElement(), marker, BUTTERFLY_TANKSTELLEN_POSITIONS[i].TABLE_Tankstellenname)
	end
end

addEventHandler("onMarkerHit", getRootElement(), function(player)
	for i, marker in ipairs(BUTTERFLY_TANKSTELLEN_MARKER) do
		if marker == source then
			setElementInterior(player, 10)
			setElementPosition(player, 6.0780, -28.6330, 1003.5490)
		end
	end
	
	if tankstelle_shop_marker == source then
		outputChatBox("Welcome")
	end
end)